package Test;

import entity.Person;

import java.lang.reflect.Method;

public class AccessPersonMethods {
    public static void main(String[] args) {
        try {
            Class clz = Class.forName("entity.Person");

            Method getAge = clz.getDeclaredMethod("getAge", null);

            getAge.setAccessible(true);

            Object returnAge = getAge.invoke(null, null);
            System.out.println("年龄是：" + returnAge);

            Object person = clz.newInstance();

            Method si = clz.getDeclaredMethod("sil", null);
            si.setAccessible(true);

            Method setName = clz.getDeclaredMethod("setName", String.class);

            setName.invoke(person, "New Person");

            Object ret = clz.getDeclaredMethod("getName").invoke(person);
            System.out.println("刚才设置的name是：" + returnAge);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}